Example of load model for detections

In [1]:
import time
import os
import argparse
import json
import cv2
import sys
sys.path += [os.path.abspath('../keras-yolo3-master')]

from utils.utils import get_yolo_boxes, makedirs
from utils.bbox import draw_boxes
from tensorflow.keras.models import load_model
from tqdm import tqdm
import numpy as np
import matplotlib.pyplot as plt

#detection_orto(infer_model, orto_image, div = [5,5], net_h = net_h, net_w = net_w, anchors
 #              obj_thresh = obj_thresh, nms_thresh = nms_thresh)
    

    
    
def detection_orto (infer_model, orto_image, div, net_h, net_w, anchors, obj_thresh, nms_thresh):
    
    div_h, div_w = div
    
    new_shape = [int(orto_image.shape[0] / div_h), int(orto_image.shape[1] / div_w)]
    
    final_boxes = []
    
    for h in range(div_h):
        for w in range(div_w):
            
            image = orto_image[new_shape[0]*h: new_shape[0]*(h + 1), new_shape[1]*w : new_shape[1]*(w + 1)]
            boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, anchors, obj_thresh, nms_thresh)[0]
            
            for box in boxes:
                box.xmin += new_shape[1]*w
                box.xmax += new_shape[1]*w
                box.ymin += new_shape[0]*h
                box.ymax += new_shape[0]*h
                final_boxes.append(box)
    return final_boxes

ORTO PHOTO

In [2]:
#image_path = 'odm_orthophoto.tif' 
image_path = 'ortomosaico_corregido.tif' 
image = cv2.imread(image_path)

#image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
#cv2.imwrite(image_path, image)

div = (8, 3) # divide row, column


fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Original Image')
plt.imshow(image, cmap='gray')
ax.axis('off') 

i = 1
sub_image = image[int(image.shape[0] / div[0]) * i : int(image.shape[0] / div[0]) * (i + 1),
                  int(image.shape[1] / div[1]) * i:int(image.shape[1] / div[1]) * (i + 1)]
fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Path Image')
plt.imshow(sub_image, cmap='gray')
ax.axis('off') 
Out[2]:
(-0.5, 459.5, 533.5, -0.5)

Load Trained Model Soiling Fault

In [3]:
## Config of trained model, change this for use different trained model
config_path  = 'config_full_yolo_fault_1_infer.json' 

with open(config_path) as config_buffer:
        config = json.load(config_buffer)
                

###############################
#####   Load the model   ######
###############################
os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus']
infer_model = load_model(config['train']['saved_weights_name'])

## Parameters of detection
net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster
obj_thresh, nms_thresh = 0.5, 0.45


#infer_model.summary()
WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.

Detection Soling Fault

In [4]:
image = cv2.imread(image_path)

## Show original image
fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Original Image')
plt.imshow(image, cmap='gray')
ax.axis('off') 

start = time.time()
## predict the bounding boxes
#boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0]
boxes = detection_orto(infer_model, orto_image = image, div = div, net_h = net_h, net_w = net_w, 
               anchors = config['model']['anchors'], obj_thresh = obj_thresh, nms_thresh = nms_thresh)
print('Elapsed time = {}'.format(time.time() - start))
## draw bounding boxes on the image using labels
draw_boxes(image, boxes, config['model']['labels'], obj_thresh)


## Show Detection Fault
fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Detection Soiling Fault')
plt.imshow(image, cmap='gray')
ax.axis('off') 
#plt.imsave('Diode Fault_6.png', image)
Elapsed time = 41.92974853515625
Out[4]:
(-0.5, 1379.5, 4272.5, -0.5)

Load Trained Model Diode Fault

In [5]:
## Config of trained model, change this for use different trained model
config_path  = 'config_full_yolo_fault_4_infer.json' 

with open(config_path) as config_buffer:
        config = json.load(config_buffer)
                

###############################
#####   Load the model   ######
###############################
os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus']
infer_model = load_model(config['train']['saved_weights_name'])

#infer_model.summary()

## Parameters of detection
net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster
obj_thresh, nms_thresh = 0.5, 0.45
WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.

Detection Diode Fault

In [6]:
image = cv2.imread(image_path)


## Show original image
fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Original Image')
plt.imshow(image, cmap='gray')
ax.axis('off') 

start = time.time()
## predict the bounding boxes
#boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0]
boxes = detection_orto(infer_model, orto_image = image, div = div, net_h = net_h, net_w = net_w, 
               anchors = config['model']['anchors'], obj_thresh = obj_thresh, nms_thresh = nms_thresh)
print('Elapsed time = {}'.format(time.time() - start))
## draw bounding boxes on the image using labels
draw_boxes(image, boxes, config['model']['labels'], obj_thresh)


## Show Detection Fault
fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Detection Diode Fault')
plt.imshow(image, cmap='gray')
ax.axis('off') 
Elapsed time = 42.96347713470459
Out[6]:
(-0.5, 1379.5, 4272.5, -0.5)

Load Trained Model Cell Damage

In [7]:
## Config of trained model, change this for use different trained model
config_path  = 'config_full_yolo_fault_2_infer.json' 

with open(config_path) as config_buffer:
        config = json.load(config_buffer)
                

###############################
#####   Load the model   ######
###############################
os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus']
infer_model = load_model(config['train']['saved_weights_name'])

#infer_model.summary()

## Parameters of detection
net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster
obj_thresh, nms_thresh = 0.5, 0.45
WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.

Detection Cell Damage Fault

In [8]:
image = cv2.imread(image_path)


## Show original image
fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Original Image')
plt.imshow(image, cmap='gray')
ax.axis('off') 

start = time.time()
## predict the bounding boxes
#boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0]
boxes = detection_orto(infer_model, orto_image = image, div = div, net_h = net_h, net_w = net_w, 
               anchors = config['model']['anchors'], obj_thresh = obj_thresh, nms_thresh = nms_thresh)
print('Elapsed time = {}'.format(time.time() - start))
## draw bounding boxes on the image using labels
draw_boxes(image, boxes, config['model']['labels'], obj_thresh)


## Show Detection Fault
fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Detection Cell damage Fault')
plt.imshow(image, cmap='gray')
ax.axis('off') 
Elapsed time = 48.806830406188965
Out[8]:
(-0.5, 1379.5, 4272.5, -0.5)

Load Trained Model Panel Disconnect

In [9]:
## Config of trained model, change this for use different trained model
config_path  = 'config_full_yolo_panel_infer.json' 

with open(config_path) as config_buffer:
        config = json.load(config_buffer)
                

###############################
#####   Load the model   ######
###############################
os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus']
infer_model = load_model(config['train']['saved_weights_name'])

#infer_model.summary()

## Parameters of detection
net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster
obj_thresh, nms_thresh = 0.5, 0.3
WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.

Detection Panel Disconnect

In [10]:
sys.path.insert(1, '../')
from panel_disconnect import disconnect

image = cv2.imread(image_path)


## Show original image
fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Original Image')
plt.imshow(image, cmap='gray')
ax.axis('off') 

start = time.time()
## predict the bounding boxes
#boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0]
boxes = detection_orto(infer_model, orto_image = image, div = div, net_h = net_h, net_w = net_w, 
               anchors = config['model']['anchors'], obj_thresh = obj_thresh, nms_thresh = nms_thresh)
boxes_panel = [box for box in boxes if box.get_score() > obj_thresh]

boxes_disc = disconnect(image, boxes_panel, z_thresh = 1.8)
print('Elapsed time = {}'.format(time.time() - start))
## draw bounding boxes on the image using labels
draw_boxes(image, boxes_disc, config['model']['labels'], obj_thresh)


## Show Detection Fault
fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Detection Fault')
plt.imshow(image, cmap='gray')
ax.axis('off') 
Elapsed time = 47.20072412490845
../panel_disconnect.py:23: RuntimeWarning: invalid value encountered in true_divide
  z_score = np.sum(image[np.int(ymin):np.int(ymax), np.int(xmin):np.int(xmax)]) / area
Out[10]:
(-0.5, 1379.5, 4272.5, -0.5)

All in one

In [14]:
## Config of trained model, change this for use different trained model



list_config_path  = ['config_full_yolo_fault_1_infer.json', 
                'config_full_yolo_fault_2_infer.json', 
                'config_full_yolo_fault_4_infer.json', 
                'config_full_yolo_panel_infer.json']

list_name = ['soiling fault', 'diode fault', 'cell damage fault', 'panel disconnected']

image = cv2.imread(image_path)

## Show original image
fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Original Image')
plt.imshow(image, cmap='gray')
ax.axis('off') 

for name, config_path in zip(list_name, list_config_path):
    
    with open(config_path) as config_buffer:
            config = json.load(config_buffer)


    ###############################
    #####   Load the model   ######
    ###############################
    os.environ['CUDA_VISIBLE_DEVICES'] = config['train']['gpus']
    infer_model = load_model(config['train']['saved_weights_name'])

    ## Parameters of detection
    net_h, net_w = 416, 416 # a multiple of 32, the smaller the faster
    obj_thresh, nms_thresh = 0.5, 0.3

    start = time.time()
    ## predict the bounding boxes
    #boxes = get_yolo_boxes(infer_model, [image], net_h, net_w, config['model']['anchors'], obj_thresh, nms_thresh)[0]
    boxes = detection_orto(infer_model, orto_image = image, div = div, net_h = net_h, net_w = net_w, 
                   anchors = config['model']['anchors'], obj_thresh = obj_thresh, nms_thresh = nms_thresh)
    
    if name == 'panel disconnected':
        
        boxes_panel = [box for box in boxes if box.get_score() > obj_thresh]
        boxes = disconnect(image, boxes_panel, z_thresh = 1.8)
        
    print('Elapsed time = {}'.format(time.time() - start))
    ## draw bounding boxes on the image using labels
    
    draw_boxes(image, boxes, config['model']['labels'], obj_thresh)


## Show Detection Fault

fig, ax = plt.subplots(figsize=(16, 16))
ax.set_title('Detection')
plt.imshow(image, cmap='gray')
ax.axis('off') 
WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.
Elapsed time = 38.81862783432007
WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.
Elapsed time = 44.0728874206543
WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.
Elapsed time = 43.44801712036133
WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.
Elapsed time = 48.66787624359131
../panel_disconnect.py:23: RuntimeWarning: invalid value encountered in true_divide
  z_score = np.sum(image[np.int(ymin):np.int(ymax), np.int(xmin):np.int(xmax)]) / area
Out[14]:
(-0.5, 1379.5, 4272.5, -0.5)
In [12]:
list_config_path.index(config_path)
Out[12]:
1
In [ ]:
 
In [ ]: